//
//  SelectPicExampleViewController.m
//  SDKSample
//
//  Created by 小肥羊 on 15/6/29.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "SelectPicExampleViewController.h"

@interface SelectPicExampleViewController ()

@end

@implementation SelectPicExampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.confirm = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80)/2, 100, 80, 30)];
    [self.confirm setTitle:@"确认选择" forState:UIControlStateNormal];
    [self.confirm setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.confirm addTarget:self action:@selector(confirmPicture) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)upPicture:(UIButton *)sender {
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"设置照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍摄新相片",@"从照片库选择", nil];
    }
    else
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"设置照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从照片库选择", nil];
    }
    [sheet showInView:self.view];
}

- (void)confirmPicture
{
    [self.pictureImage setImage:[self.cropImageView cropImage]];
    [self.cropImageView removeFromSuperview];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUInteger sourceType = 0;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 0: // 拍摄新相片
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1: // 从照片库选择
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2: // 取消
                return;
        }
    }
    else {
        if (buttonIndex == 0) {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
        else {  // 取消
            return;
        }
    }
    [self loadImageWithSourceType:sourceType];
}

- (void)loadImageWithSourceType:(UIImagePickerControllerSourceType)type
{
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, nil];
    imagePickerController.sourceType = type;
    [self presentViewController:imagePickerController animated:YES completion:^{}];
    
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:^{
        self.cropImageView = [[XFYCropImageView alloc] initWithFrame:self.view.bounds];
        [self.cropImageView setCropSize:CGSizeMake(self.view.frame.size.width - 10, (self.view.frame.size.width - 10 )/1.6)];
        [self.cropImageView setImage:image];
        [self.cropImageView addSubview:self.confirm];
        [self.view addSubview:self.cropImageView];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
